package com.lightheart.sphr.xiaobinggan.timeselection.ui

import android.view.Gravity
import android.view.MotionEvent
import android.widget.Toast
import com.lightheart.sphr.xiaobinggan.R
import com.lightheart.sphr.xiaobinggan.base.BaseActivity
import com.lightheart.sphr.xiaobinggan.module.home.presenter.HomePresenter
import com.lightheart.sphr.xiaobinggan.timeselection.uitls.TimerManager
import kotlinx.android.synthetic.main.activity_timer.*
import kotlinx.android.synthetic.main.common_toolbar.*
import kotlinx.android.synthetic.main.layout_dou.*
import kotlinx.coroutines.runBlocking
import java.util.*

/****
 * https://blog.csdn.net/qq912098812/article/details/79203032?utm_source=blogxgwz0
 *
 */
class TimerActivity : BaseActivity<HomePresenter>() {

    private var timer: Timer? = null
    private var task: TimerTask? = null
    private var minuteTime = 0

    override fun getLayoutId() = R.layout.activity_timer

    override fun initInjector() {
    }

    override fun initView() {
        initToolbar(common_toolbar, common_toolbar_title_tv, bt_sub, R.string.window, false, R.string.register)
        button.setOnClickListener {
            showToast("点击了按钮")
        }
    }

    private fun showToast(text: String) {
        val toast = Toast.makeText(this@TimerActivity, text, Toast.LENGTH_SHORT)
        toast.setGravity(Gravity.CENTER, 0, 0)
        toast.show()
    }

    override fun onResume() {
        super.onResume()
        startTimer()
    }

    private fun initTimer() {//dispatchTouchEvent
        // 初始化计时器
        task = MyTask()
        timer = Timer()
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        when (ev?.action) {
            MotionEvent.ACTION_DOWN ->{ //有按下动作时取消定时
                stopTimer()
                TimerManager.instance.stopTimer(c_progress.progress)
            }

            MotionEvent.ACTION_UP -> {//抬起时启动定时
                startTimer()
                TimerManager.instance.startTimer()
            }
        }
        return super.dispatchTouchEvent(ev)

    }

    inner class MyTask : TimerTask() {
        override fun run() {
            // 初始化计时器
            runOnUiThread {
                minuteTime++
                timeTv.text = minuteTime.toString()
                if (minuteTime == 180) {
                    //在这里弹窗然后停止计时
                    showToast("你去哪里了?")
                    stopTimer()
                }
            }
        }
    }

    private fun startTimer() {
        //启动计时器
        /**
         * java.util.Timer.schedule(TimerTask task, long delay, long period)：
         * 这个方法是说，delay/1000秒后执行task,然后进过period/1000秒再次执行task，
         * 这个用于循环任务，执行无数次，当然，你可以用timer.cancel();取消计时器的执行。
         */
        initTimer()
        try {
            timer?.schedule(task, 1000, 1000)
        } catch (e: IllegalStateException) {
            e.printStackTrace()
            initTimer()
            timer?.schedule(task, 1000, 1000)
        }
    }

    private fun stopTimer() {
        if (timer != null) {
            timer?.cancel()
        }
        minuteTime = 0
        if (timeTv != null) {
            timeTv.text = minuteTime.toString()
        }
    }

    override fun onPause() {
        super.onPause()
        //当activity不在前台是停止定时
        stopTimer()
    }

    override fun onDestroy() {
        //销毁时停止定时
        stopTimer()
        super.onDestroy()
    }

    override fun showHomeAsUp(): Boolean {
        return true
    }

}